﻿$libfx0 = @"
    [StructLayout(LayoutKind.Sequential)]
    public struct Complex
    {
      public double re;
      public double im;
    };
    [DllImport("libfx0.dll", CharSet=CharSet.Auto, SetLastError=true)]
        public static extern void TryComplex(Complex inputVar, ref Complex outputVar, int n, [In, Out] Complex [] ComplexArray);
"@  ###                                                                                          ^
    ###                                                                                          |
    ###                                               Attention: Must use [In, Out] when C function has Structure-Array variable

Add-Type -MemberDefinition $libfx0 -Namespace fx0 -Name "st_fx"

[int]$n=2;
$inputVar=New-Object fx0.st_fx+Complex -Property @{
    re = 0.0;
    im = 0.0;
};

$outputVar=New-Object fx0.st_fx+Complex -Property @{
    re = 0.0;
    im = 0.0;
};

$ComplexArray=New-Object fx0.st_fx+Complex[] $n;  ### Declare Object-Array variable
for($i=0;$i-lt$n;$i++){$ComplexArray[$i].re=0.0; $ComplexArray[$i].im=0.0;}  ### Object-Array initialized

$inputVar.re=1.0; $inputVar.im=2.0;
[fx0.st_fx]::TryComplex($inputVar, [ref]$outputVar, 2, $ComplexArray);

Write-Host "inputVar: "$inputVar.re","$inputVar.im
Write-Host "outputVar: "$outputVar.re","$outputVar.im

Write-Host "ComplexArray ->"
Write-Host $ComplexArray[0].re","$ComplexArray[0].im
Write-Host $ComplexArray[1].re","$ComplexArray[1].im